"""
空间占用检测相关的数据模型
"""
from pydantic import BaseModel, Field
from typing import List, Dict, Optional, Tuple, Any
from datetime import datetime
from enum import Enum


class OccupancyStatus(str, Enum):
    """占用状态枚举"""
    OCCUPIED = "occupied"
    VACANT = "vacant"
    PARTIALLY_OCCUPIED = "partially_occupied"
    UNKNOWN = "unknown"


class SpaceType(str, Enum):
    """空间类型枚举"""
    CLASSROOM = "classroom"
    OFFICE = "office"
    MEETING_ROOM = "meeting_room"
    LIBRARY = "library"
    LABORATORY = "laboratory"
    CAFETERIA = "cafeteria"
    AUDITORIUM = "auditorium"
    CORRIDOR = "corridor"


class EnergyRecommendationType(str, Enum):
    """节能建议类型枚举"""
    LIGHTING_OFF = "lighting_off"
    HVAC_ADJUST = "hvac_adjust"
    EQUIPMENT_STANDBY = "equipment_standby"
    VENTILATION_REDUCE = "ventilation_reduce"
    GENERAL_ENERGY_SAVE = "general_energy_save"


class SpaceOccupancyRequest(BaseModel):
    """空间占用检测请求"""
    camera_id: str = Field(..., description="摄像头ID")
    space_id: str = Field(..., description="空间ID")
    image_data: Optional[str] = Field(None, description="Base64编码的图像数据")


class SpaceOccupancyResponse(BaseModel):
    """空间占用检测响应"""
    detection_id: str = Field(..., description="检测ID")
    camera_id: str = Field(..., description="摄像头ID")
    space_id: str = Field(..., description="空间ID")
    timestamp: datetime = Field(..., description="检测时间")
    occupancy_status: OccupancyStatus = Field(..., description="占用状态")
    occupancy_rate: float = Field(..., ge=0, le=1, description="占用率(0-1)")
    person_count: int = Field(..., ge=0, description="人员数量")
    capacity: int = Field(..., gt=0, description="空间容量")
    space_type: SpaceType = Field(..., description="空间类型")
    confidence: float = Field(..., ge=0, le=1, description="检测置信度(0-1)")
    detected_activities: List[Dict[str, Any]] = Field(default_factory=list, description="检测到的活动")
    energy_recommendations: List[Dict[str, Any]] = Field(default_factory=list, description="节能建议")
    usage_duration: Optional[int] = Field(None, description="使用持续时间(分钟)")


class SpaceConfigRequest(BaseModel):
    """空间配置请求"""
    space_id: str = Field(..., description="空间ID")
    space_name: str = Field(..., description="空间名称")
    space_type: SpaceType = Field(..., description="空间类型")
    capacity: int = Field(..., gt=0, description="空间容量")
    area_sqm: float = Field(..., gt=0, description="空间面积(平方米)")
    operating_hours: Dict[str, Tuple[str, str]] = Field(..., description="工作时间")
    energy_saving_enabled: bool = Field(True, description="是否启用节能模式")
    occupancy_threshold: float = Field(0.1, ge=0, le=1, description="占用阈值")
    vacancy_timeout_minutes: int = Field(30, gt=0, description="空闲超时时间(分钟)")


class SpaceConfigResponse(BaseModel):
    """空间配置响应"""
    space_id: str = Field(..., description="空间ID")
    space_name: str = Field(..., description="空间名称")
    space_type: SpaceType = Field(..., description="空间类型")
    capacity: int = Field(..., description="空间容量")
    area_sqm: float = Field(..., description="空间面积(平方米)")
    operating_hours: Dict[str, Tuple[str, str]] = Field(..., description="工作时间")
    energy_saving_enabled: bool = Field(..., description="是否启用节能模式")
    occupancy_threshold: float = Field(..., description="占用阈值")
    vacancy_timeout_minutes: int = Field(..., description="空闲超时时间(分钟)")


class SpaceUsageStatsResponse(BaseModel):
    """空间使用统计响应"""
    space_id: str = Field(..., description="空间ID")
    total_detections: int = Field(..., description="总检测次数")
    average_occupancy_rate: float = Field(..., description="平均占用率")
    peak_occupancy: float = Field(..., description="峰值占用率")
    utilization_hours: float = Field(..., description="利用时间(小时)")
    energy_savings_potential: float = Field(..., description="节能潜力百分比")
    status_distribution: Dict[str, int] = Field(..., description="状态分布")
    period_days: int = Field(..., description="统计周期(天)")


class SpaceServiceStatsResponse(BaseModel):
    """空间服务统计响应"""
    total_detections: int = Field(..., description="总检测次数")
    spaces_monitored: int = Field(..., description="监控空间数量")
    average_occupancy_rate: float = Field(..., description="平均占用率")
    energy_recommendations_generated: int = Field(..., description="生成的节能建议数量")
    vacant_spaces_count: int = Field(..., description="空闲空间数量")
    occupied_spaces_count: int = Field(..., description="占用空间数量")


class SpaceOccupancyListRequest(BaseModel):
    """空间占用检测列表请求"""
    space_id: Optional[str] = Field(None, description="空间ID")
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    limit: int = Field(100, ge=1, le=1000, description="返回数量限制")


class SpaceUsageStatsRequest(BaseModel):
    """空间使用统计请求"""
    space_id: str = Field(..., description="空间ID")
    days: int = Field(7, ge=1, le=365, description="统计天数")


class EnergyRecommendationRequest(BaseModel):
    """节能建议请求"""
    space_id: Optional[str] = Field(None, description="空间ID")
    hours: int = Field(24, ge=1, le=168, description="时间范围(小时)")


# 响应包装器
class SpaceOccupancyListResponse(BaseModel):
    """空间占用检测列表响应"""
    detections: List[SpaceOccupancyResponse] = Field(..., description="检测结果列表")
    total: int = Field(..., description="总数量")


class SpaceConfigListResponse(BaseModel):
    """空间配置列表响应"""
    configs: List[SpaceConfigResponse] = Field(..., description="配置列表")
    total: int = Field(..., description="总数量")


class EnergyRecommendationListResponse(BaseModel):
    """节能建议列表响应"""
    recommendations: List[Dict[str, Any]] = Field(..., description="节能建议列表")
    total: int = Field(..., description="总数量")